package com.sina.shop.card;

import com.sina.shop.domain.card.Card;
import com.sina.shop.service.card.CardService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-02-07-17:42
 */
@RestController
@RequestMapping("/card")
public class CardController extends BaseController {

    @Autowired
    @Qualifier("cardService")
    private CardService cardService;

    @Autowired
    private DiscoveryClient discoveryClient;

    @RequestMapping(value = "/getCardListByAdminId", method = RequestMethod.GET)
    public List<Card> getCardListByAdminId(@RequestParam(value = "adminId") Long adminId) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/getCardListByAdminId, method: get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return cardService.getCardListByAdminId(adminId);
    }

    @RequestMapping(value = "/getCard", method = RequestMethod.GET)
    public Card getCard(@RequestParam(value = "id") Long id) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/getCard, method: get, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return cardService.getCardById(id);
    }

    @RequestMapping(value = "/deleteCard", method = RequestMethod.POST)
    public Integer deleteCard(Long id) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/deleteCard, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return cardService.deleteCard(id);
    }

    @RequestMapping(value = "/saveCard", method = RequestMethod.POST)
    public Card saveCard(@RequestBody(required = false) Card card) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/saveCard, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return cardService.saveCard(card);
    }

    @RequestMapping(value = "/updateCard", method = RequestMethod.POST)
    public Card updateCard(@RequestBody(required = false) Card card) {
        ServiceInstance instance = discoveryClient.getLocalServiceInstance();
        LOG.info("/updateBusiness, method: post, host:" + instance.getHost() + ", serviceId:" + instance.getServiceId() + ",time: " + LocalDateTime.now());
        return cardService.updateCard(card);
    }
}
